import Vue from 'vue'
import store from '../store/index.js'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Cate from '../components/goods/Cate.vue'
import GoodList from '../components/goods/List.vue'
import Providers from '../components/customer/Providers.vue'
import Customer from '../components/customer/Customer.vue'
import Order from '../components/order/Order.vue'
import SaleOrder from '../components/order/SaleOrder.vue'
import Report from '../components/report/Report.vue'
import Report2 from '../components/report/Report2.vue'
import Lock from '../components/Lock.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path:'/lock',
    component:Lock
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/adminUser',
        component: Users
      },
      {
        path: '/customerList',
        component: Customer
      },
      {
        path: '/goodsCategory',
        component: Cate
      },
      {
        path: '/goodsList',
        component: GoodList
      },
      {
        path:'/providerList',
        component:Providers
      },
      {
        path: '/saleList',
        component: Order
      },
      {
        path: '/finishSaleList',
        component: SaleOrder
      },
      {
        path: '/dataView',
        component: Report
      },
      {
        path: '/dataView2',
        component: Report2
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径，from代表从哪个路径跳转，next是一个函数，表示放行
  // next（）放行 next（'/login'）强制跳转
  if (to.path === '/login') return next()
  //锁屏页验证
  if(store.state.lock==true && to.path !== '/lock') return next('/lock')
  //token验证
  const token=window.sessionStorage.getItem("token");
  if(!token) return next('/login')
  next()
})

export default router
